package org.jypj.dev.service.impl;


import java.util.*;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jypj.dev.constant.JpushConstant;
import org.jypj.dev.util.work.GiveWorkUtil;
import org.jypj.dev.vo.hxVo.PushObject;
import org.jypj.dev.util.*;
import org.springframework.stereotype.Service;
import org.jypj.dev.model.GiveWork;
import org.jypj.dev.dao.IGiveWorkDao;
import org.jypj.dev.service.IGiveWorkService;
import org.jypj.dev.util.Page;

@Service("giveWorkService")
public class GiveWorkServiceImpl implements IGiveWorkService {

    @Resource
    private IGiveWorkDao giveWorkDao;


    /**
     * 保存
     * 所有字段全都保存
     *
     * @param giveWork
     * @return 保存后的对象包括ID
     */
    public int saveGiveWork(GiveWork giveWork) {
        if (StringUtil.isEmpty(giveWork.getId())) {
            String uuid = UUID.randomUUID().toString().replace("-", "").trim();
            giveWork.setId(uuid);
        }
        return giveWorkDao.saveGiveWork(giveWork);
    }

    /**
     * 保存
     * 所有字段全都保存
     *
     * @param giveWorks
     * @return 保存后的对象包括ID
     */
    public int saveGiveWorks(List<GiveWork> giveWorks, String giveWorkId) {
        if (StringUtil.isNotEmpty(giveWorkId)) {
            GiveWork giveWork = giveWorkDao.selectGiveWorkById(giveWorkId);
            giveWorkDao.deleteGiveWorkByObject(giveWork);
        }
        for (GiveWork giveWork : giveWorks) {
            giveWorkDao.saveGiveWork(giveWork);
        }
        return 0;
    }

    /**
     * 根据ID删除
     *
     * @param id 主键ID
     * @return 删除记录数
     */
    public int deleteGiveWorkById(String id) {

        return giveWorkDao.deleteGiveWorkById(id);
    }

    /**
     * 根据对象删除
     *
     * @return
     */
    public int deleteGiveWorkByObject(GiveWork giveWork) {

        return giveWorkDao.deleteGiveWorkByObject(giveWork);
    }

    /**
     * 更新
     * 更新所有字段
     *
     * @param giveWork
     * @return 保存后的对象包括ID
     */
    public int updateGiveWork(GiveWork giveWork) {

        return giveWorkDao.updateGiveWork(giveWork);
    }

    /**
     * 按ID查询
     *
     * @return GiveWork
     * @parm id 主键ID
     */
    public GiveWork selectGiveWorkById(String id) {

        return giveWorkDao.selectGiveWorkById(id);
    }

    /**
     * 分页查询 包含条件
     *
     * @param page 分页对象
     * @param map  查询条件
     * @return List<GiveWork>
     */
    public Page selectOnePageByMap(Page page, Map<String, Object> map) {
        List<GiveWork> giveWorks = giveWorkDao.selectOnePageByMap(page, map);
        if (giveWorks != null && giveWorks.size() > 0) {
            page.setResult(giveWorks);
        } else {
            page.setResult(new ArrayList<GiveWork>());
        }
        return page;
    }

    /**
     * 分页查询 包含对象条件
     *
     * @param page     分页对象
     * @param giveWork 查询条件
     * @return Page
     */
    public Page selectOnePageByGiveWork(Page page, GiveWork giveWork) {
        List<GiveWork> giveWorks = giveWorkDao.selectOnePageByGiveWork(page, giveWork);
        if (giveWorks != null && giveWorks.size() > 0) {
            page.setResult(giveWorks);
        } else {
            page.setResult(new ArrayList<GiveWork>());
        }
        return page;
    }

    /**
     * 按条件查询全部的
     *
     * @param map 查询条件
     * @return List<GiveWork>
     */
    public List<GiveWork> selectAllByMap(Map<String, Object> map) {
        return giveWorkDao.selectAllByMap(map);
    }


    /**
     * 按条件查询全部的
     *
     * @return List<GiveWork>
     */
    public List<GiveWork> selectAllByGiveWork(GiveWork giveWork) {

        return giveWorkDao.selectAllByGiveWork(giveWork);
    }

    /**
     * 按条件查询单个对象
     *
     * @param map 查询条件
     * @return GiveWork
     */
    public GiveWork selectObjectByMap(Map<String, Object> map) {

        return giveWorkDao.selectObjectByMap(map);
    }

    /**
     * 按条件查询单个对象
     *
     * @return GiveWork
     */
    public GiveWork selectObjectByGiveWork(GiveWork giveWork) {

        return giveWorkDao.selectObjectByGiveWork(giveWork);
    }

    @Override
    public void remindWork() {
        String todayString = DateUtils.getTodayString("yyyy-MM-dd");
        GiveWork giveWork = new GiveWork();
        giveWork.setWorkDate(todayString);
        List<GiveWork> giveWorks = giveWorkDao.selectAllByGiveWork(giveWork);//查询所有未叫班的用户
        Set<String> ids = GiveWorkUtil.remindWork(giveWorks);//得到要更新的IDs
        String idStr = StringUtils.join(ids, ",");
        if (StringUtil.isNotEmpty(idStr)) {
            giveWorkDao.updateIsRemind(idStr);
        }
    }
}